<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible"
             width="80%">
    <div>
      <div class="seciont-1">基本信息</div>
      <el-form label-width="120px" :model="dataForm" :rules="dataRule" ref="dataForm"  @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="运营商名称：" prop="name">
          <el-input v-model="dataForm.name" placeholder="请输入运营商名称" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="运营商LOGO" prop="logo">
          <pic-upload v-model="dataForm.logo"></pic-upload>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactsName">
          <el-input v-model="dataForm.contactsName" placeholder="请输入联系人姓名" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactsMobile">
          <el-input v-model="dataForm.contactsMobile" placeholder="请输入联系人电话" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="运营模式" prop="businessMode">
          <el-select v-model="dataForm.businessMode" style="width: 50%">
            <el-option label="圈地" value="0"></el-option>
            <el-option label="人头" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充电桩跳转地址" prop="qrcodeUrl">
          <el-input v-model="dataForm.qrcodeUrl" placeholder="请输入充电桩跳转地址" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="运营商客服电话" prop="servicePhone">
          <el-input v-model="dataForm.servicePhone" placeholder="请输入运营商客服电话" type="" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="运营商在线客服地址" prop="serviceAddress">
          <el-input v-model="dataForm.serviceAddress" placeholder="请输入运营商在线客服地址" type="" style="width: 50%"></el-input>
        </el-form-item>
        <div class="seciont-1">运营商银行信息</div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行名称" prop="bankName">
              <el-input v-model="dataForm.bankName" placeholder="请输入银行名称" type="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支行名称" prop="bankSubName">
              <el-input v-model="dataForm.bankSubName" placeholder="请输入支行名称" type="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input v-model="dataForm.bankAccount" placeholder="请输入银行账号" type="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="持卡人姓名" prop="accountName">
              <el-input v-model="dataForm.accountName" placeholder="请输入持卡人姓名" type="" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="footer">
          <el-button type="primary" @click.stop="dataFormSubmit">提交</el-button>
          <el-button type="primary" plain @click.stop="close">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import picUpload from '@/components/pic-upload/index.vue'
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    name: 'merchant-add-or-update',
    components: {
      picUpload
    },
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: '',
          name: '',
          logo: '',
          contactsName: '',
          contactsMobile: '',
          businessMode: '',
          qrcodeUrl: '',
          servicePhone: '',
          serviceAddress: '',
          bankName: '',
          bankSubName: '',
          bankAccount: '',
          accountName: ''
        },
        dataRule: {
          name: [
            { required: true, message: '运营商名称不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: '运营商LOGO不能为空', trigger: 'blur' }
          ],
          contactsName: [
            { required: true, message: '联系人姓名不能为空', trigger: 'blur' }
          ],
          contactsMobile: [
            { required: true, message: '联系人电话不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          bankName: [
            { required: true, message: '银行名称不能为空', trigger: 'blur' }
          ],
          bankSubName: [
            { required: true, message: '支行名称不能为空', trigger: 'blur' }
          ],
          bankAccount: [
            { required: true, message: '银行账号不能为空', trigger: 'blur' }
          ],
          accountName: [
            { required: true, message: '持卡人姓名不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (model) {
        if (model) {
          this.dataForm = model
        }
        this.visible = true
      },

      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(this.dataForm.id ? '/v1/admin/merchant/update' : '/v1/admin/merchant/add'),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList', this.page)
                }
              })
            })
          }
        })
      },

      close () {
        this.$refs['dataForm'].resetFields()
        this.visible = false
      }
    }
  }
</script>

<style lang="scss" scoped>
  .seciont-1{
    padding: 0px 0px 15px 0px;
    margin-bottom: 12px;
    color: #333333;
    font-weight: bold;
    border-bottom:  solid 1px #DADADA;
  }
  .footer{
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
